//
//  FieldCell.swift
//  Pods
//
//  Created by zhu xietong on 2017/8/17.
//
//
import UIKit
import Eelay

var flable_w = 90
var flable_h = 21


extension FK{
    public static var indicator       = "_indicator"
    public static var markRequire       = "_markRequif1"
    public static var indicator_more_img       = "indicator_more_img"
    
}

public enum FieldCellRight{
    case more
    case image(UIImage)
    case text(String)
    case none
}

open class SimpleFieldCell<Field:FieldView>: FieldCell {
    public var field = Field()
    open override func addLayoutRules() {
        super.addLayoutRules()
        contentView.eelay = [
            [titleL,FieldCell.name_width.+1000],
            [field,[ee.T.B],[titleL,ee.R,ee.L,8],[rightIcon,ee.L,ee.R,-8]],
        ]
    }

    open override func mo_model_reload() {
        if mo.model[FK.indicator,""].len > 0
        {
        }
//        contentView.fillData(model: mo.model)
        field.controller = self.content_controller
        if let f = DataNode<NSMutableDictionary>.path(node: "\(FK.fields).0", mo.model)
        {
            field.mo.model = f
        }
    }
}

open class FieldCell: JoTableCell {
    public let titleL = UILabel()
    public static var padding_top:CGFloat = 12;
    public static var padding_bottom:CGFloat = 12;
    public static var name_width:CGFloat = 90;
    public static var padding_left:CGFloat = 20;
    public static var padding_right:CGFloat = 20;
    public static var separator_color = UIColor._separator
    public static var backgroud_color = UIColor._cellBackground

    
    
    public var requiredMark = UILabel()
    public var rightIcon = UIButton()
    public var rightIconConstrains = [NSLayoutConstraint]()
    
    
    
    
    open override func layoutSubviews() {
        super.layoutSubviews()
    }
    public var indicator:FieldCellRight = .more
    {
        didSet{
            
            if titleL.superview == nil{
                return
            }
            rightIcon.setContentConstrainToLowLever()
            contentView.removeConstraints(rightIconConstrains)
            switch indicator {
            case .more:
                let lays = [
                    [rightIcon,"20",20,[ee.R,-FieldCell.padding_right],[titleL,ee.Y]],
                    ]
                self.rightIconConstrains = contentView.setEeLays(lays: lays).1
                rightIcon.setImage(UIImage(named:FK.indicator_more_img), for: .normal)
            case .image(let img):
                let lays = [
                    [rightIcon,"20",20,[ee.R,-FieldCell.padding_right],[titleL,ee.Y]],
                ]
                self.rightIconConstrains = contentView.setEeLays(lays: lays).1
                rightIcon.setImage(img, for: .normal)
            case .text(let text):
                let lays = [
                    [rightIcon,[ee.R,-FieldCell.padding_right],[titleL,ee.Y]],
                    ]
                self.rightIconConstrains = contentView.setEeLays(lays: lays).1
                rightIcon.setTitle(text, for: .normal)
                rightIcon.setContentConstrainToHighLever()
                
            default:
                let lays = [
                    [rightIcon,"0",0,[ee.R,-FieldCell.padding_right],[titleL,ee.Y]],
                ]
                self.rightIconConstrains = contentView.setEeLays(lays: lays).1
            }
            rightIcon.contentEdgeInsets = .zero
            
        }
    }
    
    public var showMark:Bool = false{
        didSet{
            if showMark
            {
                requiredMark.textColor = .red
            }
            else{
                requiredMark.textColor = .clear
            }
        }
    }
    
    
    open override func addLayoutRules() {
        titleL.ui.font15.cl_444.node("title")
        titleL.font = FieldView.valueStyle.font
        titleL.textColor = FieldView.valueStyle.color
        
        backgroundColor = ._cellBackground

        let w = Int(FieldCell.name_width)
        let t = Int(FieldCell.padding_top)
        let b = Int(FieldCell.padding_bottom)
        
        let l = Int(FieldCell.padding_left)
        //        let r = Int(FieldCell.padding_right)
        
        
        contentView.eelay = [
            [titleL,[ee.T.L.B,[t,l,-b]],"21",.<w],
            [requiredMark,[titleL,ee.Y,3],[titleL,ee.R,ee.L,2]],
            [__line,[ee.L.B.R,[0,0,0]],"\(TP.separatorHeight)"]
        ]
        self.indicator = .none
        
        __line.backgroundColor = ._separator
        titleL.setContentConstrainToHighLever()
        requiredMark.ui.font18.text("*").text(color: .clear)
        rightIcon.ui.font(size: 13).ncolor(hex: "#aaa")
    }
    
    open override func mo_model_willSet(new: NSMutableDictionary) {
        let markRequire = new[FK.markRequire,"NO"]
        if markRequire == "YES"
        {
            self.showMark = true
        }
        else{
            self.showMark = false
        }
        
        let indicatorInfo = new[FK.indicator,"none"]
        if indicatorInfo.hasPrefix("<image>")
        {
            let imgName = indicatorInfo.replacingOccurrences(of: "<image>", with: "")
            if let img = UIImage(named: imgName)
            {
                self.indicator = .image(img)
            }
            return
        }
        if indicatorInfo.hasPrefix("<text>")
        {
            let text = indicatorInfo.replacingOccurrences(of: "<text>", with: "")
            self.indicator = .text(text)
            
            return
        }
        if indicatorInfo.hasPrefix("<more>")
        {
            self.indicator = .more
            return
        }
        self.indicator = .none
        
    }
}

